package com.mine.androidlearn.common.base;

import android.os.Bundle;
import android.support.v4.app.Fragment;

import com.mine.androidlearn.GlobalApplication;

/**
 * mvp模式下Fragment的基类
 *
 * @author mine
 * @version V1.0 2018/8/5 16:00
 */
public abstract class BaseMvpFragment<V extends IBaseView, T extends BasePresenter> extends Fragment {

    protected GlobalApplication mGlobalApplication;
    protected T mPresenter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mGlobalApplication = (GlobalApplication) getActivity().getApplication();

        // 创建Presenter
        mPresenter = createPresenter();
    }

    @SuppressWarnings("unchecked")
    @Override
    public void onResume() {
        super.onResume();
        //关联View
        mPresenter.attachView((V) this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

        mPresenter.unsubscribe();
        //解除关联View
        mPresenter.detachView();

        // 加入内存泄漏监视队列
        GlobalApplication.getRefWatcher().watch(this);

    }




    /**
     * 创建Presenter
     */
    public abstract T createPresenter();
}
